/*
 * File Name:service_test.c
 * Version:v1.0.0
 * Change Logs:
 * Date             Author      	main change
 * 2025-03-05      	lanxin          the first version
 * 2025-03-17      	lanxin          rename function init to srv_init...
 *
*/

/********************************************* READ ME BEGIN *************************************************************/

/* Brief:
 *
 * *1.mal_service 测试程序。
 *
 */

/* Notice:
 *
 * *
 *
 */

/********************************************* READ ME END ***************************************************************/

/* source file */
#include "mal_service.h"

/* debug define */

/* private define */

/* function declaration */

/* variable declaration */
enum
{
	E_POS_READ_1 = 1,
	E_POS_READ_2,
	E_POS_WRITE_1,
	E_POS_WRITE_2,
	E_POS_CONTROL_1,
	E_POS_CONTROL_2,
	E_EVENT_1,
	E_EVENT_2,
};

static s_service_t srv_1, srv_2;
static char *srv_name_1 = "srv_1";
static char *srv_name_2 = "srv_2";
static int state_1 = 1, state_2 = 1;

static int srv_init ( const struct service *srv, void *para )
{
	rt_kprintf ( "do srv [%s] init,para is %s\r\n", srv->name, ( char * ) para );
	return 0;
}

static int srv_stop ( const struct service *srv )
{
	rt_kprintf ( "do srv [%s] stop\r\n", srv->name );
	return 0;
}

static int srv_read ( const struct service *srv, int pos, void *p_read, int read_size )
{
	rt_kprintf ( "do srv [%s] read\r\n", srv->name );

	if ( E_POS_READ_1 == pos )
	{
		* ( int * ) p_read = state_1;
		return 1;
	}
	else if ( E_POS_READ_2 == pos )
	{
		* ( int * ) p_read = state_2;
		return 1;
	}

	return 0;
}

static int srv_write ( const struct service *srv, int pos, const void *p_write, int write_size )
{
	rt_kprintf ( "do srv [%s] write\r\n", srv->name );

	if ( E_POS_WRITE_1 == pos )
	{
		state_1 = * ( int * ) p_write;
		return 1;
	}
	else if ( E_POS_WRITE_2 == pos )
	{
		state_2 = * ( int * ) p_write;
		return 1;
	}

	return 0;
}

static int srv_control ( const struct service *srv, int cmd, void *argv )
{
	rt_kprintf ( "do srv [%s] control\r\n", srv->name );

	if ( E_POS_CONTROL_1 == cmd || E_POS_CONTROL_2 == cmd )
	{
		rt_kprintf ( "srv [%s] control msg %s\r\n", srv->name, ( char * ) argv );
		return 0;
	}

	return -1;
}

static int usr_cb ( const s_service_t *srv, int event, const void *argv )
{
	rt_kprintf ( "do srv [%s] usr_cb\r\n", srv->name );

	if ( event == E_EVENT_1 || event == E_EVENT_2 )
	{
		rt_kprintf ( "usr cb msg:%s\r\n", ( char * ) argv );
	}

	return 0;
}

static int do_find ( void )
{
	const s_service_t *_srv1 = mal_service_find ( srv_name_1 );
	const s_service_t *_srv2 = mal_service_find ( srv_name_2 );
	const s_service_t *_srv3 = mal_service_find ( "srv" );

	if ( _srv1 )
		rt_kprintf ( "find srv %s\r\n", _srv1->name );

	if ( _srv2 )
		rt_kprintf ( "find srv %s\r\n", _srv2->name );

	return 0;
}

static int do_init ( void )
{
	mal_service_init ( mal_service_find ( srv_name_1 ), "init srv_1" );
	mal_service_init_by_name ( srv_name_2, "init srv_2" );

	return 0;
}

static int do_stop ( void )
{
	mal_service_stop ( mal_service_find ( srv_name_1 ) );
	mal_service_stop_by_name ( srv_name_2 );
	mal_service_stop_all();

	return 0;
}

static int do_read ( void )
{
	int state_1 = 0, state_2 = 0;

	int res1 = mal_service_read ( mal_service_find ( srv_name_1 ), E_POS_READ_1, ( void * ) &state_1, 1 );
	rt_kprintf ( "srv 1 read res:%s val:%d\r\n", res1 == 1 ? "ok" : "failed", state_1 );

	int res2 = mal_service_read_by_name ( srv_name_2, E_POS_READ_2, ( void * ) &state_2, 1 );
	rt_kprintf ( "srv 2 read res:%s val:%d\r\n", res2 == 1 ? "ok" : "failed", state_2 );

	int res3 = mal_service_read_by_name ( srv_name_2, 3, ( void * ) &state_2, 1 );
	rt_kprintf ( "srv 2 read res:%s val:%d\r\n", res3 == 1 ? "ok" : "failed", state_2 );

	return 0;
}

static int do_write ( void )
{
	int state_1 = 3, state_2 = 4;

	int res1 = mal_service_write ( mal_service_find ( srv_name_1 ), E_POS_WRITE_1, ( void * ) &state_1, 1 );
	rt_kprintf ( "srv 1 write res:%s val:%d\r\n", res1 == 1 ? "ok" : "failed", state_1 );

	int res2 = mal_service_write_by_name ( srv_name_2, E_POS_WRITE_2, ( void * ) &state_2, 1 );
	rt_kprintf ( "srv 2 write res:%s val:%d\r\n", res2 == 1 ? "ok" : "failed", state_2 );

	int res3 = mal_service_write_by_name ( srv_name_2, 5, ( void * ) &state_2, 1 );
	rt_kprintf ( "srv 2 write res:%s val:%d\r\n", res3 == 1 ? "ok" : "failed", state_2 );

	return 0;
}

static int do_control ( void )
{
	mal_service_control ( &srv_1, E_POS_CONTROL_1, "control 1" );
	mal_service_control_by_name ( srv_name_2, E_POS_CONTROL_2, "control 2" );

	return 0;
}

static int do_cb ( void )
{
	mal_service_set_usr_cb ( &srv_1, usr_cb );
	mal_service_set_usr_cb_by_name ( srv_name_2, usr_cb );

	if ( srv_1.usr_cb )
		srv_1.usr_cb ( &srv_1, E_EVENT_1, "cb 1" );

	if ( srv_2.usr_cb )
		srv_2.usr_cb ( &srv_2, E_EVENT_2, "cb 2" );

	return 0;
}

static int do_usr_data ( void )
{
	mal_service_set_usr_data ( &srv_1, &srv_1 );
	mal_service_set_usr_data_by_name ( srv_name_2, &srv_2 );

	rt_kprintf ( "srv 1 usr data:%s\r\n", ( ( s_service_t * ) srv_1.usr_data )->name );
	rt_kprintf ( "srv 2 usr data:%s\r\n", ( ( s_service_t * ) srv_2.usr_data )->name );

	return 0;
}

static int service_init ( void )
{
	rt_snprintf ( srv_1.name, SERVICE_NAME_MAX, srv_name_1 );
	rt_snprintf ( srv_2.name, SERVICE_NAME_MAX, srv_name_2 );

	srv_1.init = srv_init;
	srv_1.stop = srv_stop;
	srv_1.read = srv_read;
	srv_1.write = srv_write;
	srv_1.control = srv_control;

	srv_2.init = srv_init;
	srv_2.stop = srv_stop;
	srv_2.read = srv_read;
	srv_2.write = srv_write;
	srv_2.control = srv_control;

	mal_service_register ( &srv_1 );
	mal_service_register ( &srv_2 );

	return 0;
}

/* 导出到初始化列表 */
INIT_APP_EXPORT ( service_init );

/* 导出到 MSH 命令列表 */
MSH_CMD_EXPORT ( do_find, find );
MSH_CMD_EXPORT ( do_init, init );
MSH_CMD_EXPORT ( do_stop, stop );
MSH_CMD_EXPORT ( do_control, control );
MSH_CMD_EXPORT ( do_read, read );
MSH_CMD_EXPORT ( do_write, write );
MSH_CMD_EXPORT ( do_cb, cb );
MSH_CMD_EXPORT ( do_usr_data, usr_data );